/*******************************************************************************
 Jimm - Mobile Messaging - J2ME ICQ clone
 Copyright (C) 2003-05  Jimm Project

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ********************************************************************************
 File: src/jimm/DebugLog.java
 Version: ###VERSION###  Date: ###DATE###
 Author(s): Artyomov Denis
 *******************************************************************************/

// #sijapp cond.if modules_MAGIC_EYE is "true" #
package org.jimm.modules;

import java.util.Vector;

import org.jimm.protocol.Protocol;

import android.view.MenuItem;

public class MagicEye {
    private static final MagicEye instance = new MagicEye();
    private Vector<String> uins = new Vector<String>();
    private Vector<Protocol> protocols = new Vector<Protocol>();

    private MagicEye() {
//        MenuModel menu = new MenuModel();
//        menu.addItem("user_menu",     MENU_USER_MENU);
//        menu.addItem("copy_text",     MENU_COPY);
//        menu.addItem("copy_all_text", MENU_COPY_ALL);
//        menu.addItem("clear",         MENU_CLEAN);
//        //menu.addItem("back",          MenuModel.BACK_COMMAND_CODE);
//        menu.setActionListener(this);
//        menu.setDefaultItemCode(MENU_COPY);
//        setMenu(menu, MENU_USER_MENU);
    }

    public static void activate() {
//        instance.setCurrentItem(instance.getSize());
//        instance.show();
    }

    private synchronized void registerAction(Protocol protocol, String uin, int action, int msg) {
        uins.addElement(uin);
        protocols.addElement(protocol);
//
//        int counter = uiBigTextIndex++;
//        String date = Util.getDateString(false);
//        action = JLocale.getString(action);
//        Contact contact = protocol.getItemByUIN(uin);
//
//        Parser record = createParser(counter);
//        record.useMinHeight();
//        record.addText(date + ": ", THEME_MAGIC_EYE_NUMBER, FONT_STYLE_PLAIN);
//        if (null == contact) {
//            record.addText(uin, THEME_MAGIC_EYE_NL_USER, FONT_STYLE_PLAIN);
//        } else {
//            record.addText(contact.getName(),
//                    THEME_MAGIC_EYE_USER, FONT_STYLE_PLAIN);
//        }
//        record.doCRLF();
//        record.addText(action, THEME_MAGIC_EYE_ACTION, FONT_STYLE_PLAIN);
//        if (null != msg) {
//            record.doCRLF();
//            record.addText(msg, THEME_MAGIC_EYE_TEXT, FONT_STYLE_PLAIN);
//        }
//
//        lock();
//        addPar(record);
        removeOldRecords();
//        unlock();
    }

    private void removeOldRecords() {
        final int maxRecordCount = 50;
        while (maxRecordCount < uins.size()) {
            protocols.removeElementAt(0);
            uins.removeElementAt(0);
//            removeFirstText();
        }
    }

    //    public static final int ACTTYPE_SYSNOTICE   = 0;
//    public static final int ACT_ANTISPAM        = 1;
//    public static final int ACTTYPE_READ_STATUS = 2;
//    public static final int ACT_INVISIBLE_CHECK = 4;
//
    public static void addAction(Protocol protocol, String uin, int action, int msg) {
        instance.registerAction(protocol, uin, action, msg);
    }

    public static void addAction(Protocol protocol, String uin, int action) {
        instance.registerAction(protocol, uin, action, 0);
    }

    private static final int MENU_COPY = 0;
    private static final int MENU_COPY_ALL = 1;
    private static final int MENU_CLEAN = 2;
    private static final int MENU_USER_MENU = 3;

    public boolean onContextItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case MENU_COPY:
            case MENU_COPY_ALL:
                //copy(MENU_COPY_ALL == cmd);
                break;

            case MENU_CLEAN:
                synchronized (instance) {
                    uins.removeAllElements();
                    protocols.removeAllElements();
                }
                break;

            case MENU_USER_MENU:
//                int item = getCurrTextIndex();
//                String uin = (String)uins.elementAt(item);
//                Protocol protocol = (Protocol)protocols.elementAt(item);
//                showMenu(protocol.createTempContact(uin).getContextMenu(protocol));
                return true;
        }
        return false;
    }
}
// #sijapp cond.end#